/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import beans.AlumnoBean;
import beans.AlumnoMateriaBean;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import databeans.AlumnoDataBean;
import databeans.MateriaAlumnoDataBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import utils.respuestaServlet;

/**
 *
 * @author Emerson
 */
public class AgregarAlumno extends HttpServlet {
   
     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            AlumnoBean alumno= new AlumnoBean();
            String carnet=request.getParameter("carnet");
            alumno.setCarnet(carnet);
            alumno.setNombre(request.getParameter("nombres"));
            alumno.setSexo(request.getParameter("sexo"));
            alumno.setEmail(request.getParameter("email"));
            alumno.setTelefono(Base64.encode(request.getParameter("telefono").getBytes()));

            
            String[] materias = request.getParameterValues("materias");
            
            ArrayList <AlumnoMateriaBean> materias_alum= new ArrayList <AlumnoMateriaBean> ();
            
            
            for(int i=0; i <= materias.length - 1; i++){
               AlumnoMateriaBean aux = new AlumnoMateriaBean();
                aux.setCarnet(carnet);
                aux.setCodmateria(materias[i]);
                materias_alum.add(aux);
            }
            AlumnoDataBean addalumno = new AlumnoDataBean();
            addalumno.setAlumno(alumno);
            MateriaAlumnoDataBean mad= new MateriaAlumnoDataBean();
            mad.setMateriasAlumno(materias_alum);

            respuestaServlet.imprimirEncabezado(out, "Agregar Alumno");
            out.println("<p>Alumno agregado exitosamente</p>");
            respuestaServlet.imprimirPie(out);

        }
        catch (Exception ex) {
            Logger.getLogger(AgregarAlumno.class.getName()).log(Level.SEVERE, null, ex);
            respuestaServlet.imprimirEncabezado(out, "Agregar Alumno");
            out.println("El alumno no pudo ser guardado, verifique que no exista en el sistema");
            respuestaServlet.imprimirPie(out);

        }
        finally {
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
